﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameStepAzure.Models;
using AdminModel=GameStepAzure.Areas.Admin.Models;

namespace GameStepAzure.Areas.Admin.Controllers
{
    [Authorize(Roles="Administrador")]
    public class AdministracionController : Controller
    {
        //
        // GET: /Admin/Administracion/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(String s)
        {
            if(ModelState.IsValid){
            
            
            }
            return View();
        }


        public ActionResult AltaProducto(){
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem() {Selected=true,Text="Xbox",Value="Xbox" });
            items.Add(new SelectListItem() { Selected = false, Text = "PsVita", Value = "Psvita" });
            items.Add(new SelectListItem() { Selected = false, Text = "PS3", Value = "PS3" });
            items.Add(new SelectListItem() { Selected = false, Text = "Wii", Value = "Wii" });
            items.Add(new SelectListItem() { Selected = false, Text = "3DS", Value = "3DS" });

            ViewBag.lista = items;

            
        return View();
        }

        [HttpPost]
        public ActionResult AltaProducto(Inventario inventario)
        {
            AdminModel.IFachadaCatalogoProductos fachada = new AdminModel.CatalogoProductos();

            String path = Server.MapPath("~/_Documentos/Imagenes");
            fachada.AgregarInventario(inventario,path);

            return RedirectToAction("AltaProducto");
        }



        public ActionResult EditarProducto(int Id)
        {
            AdminModel.IFachadaCatalogoProductos fachada = new AdminModel.CatalogoProductos();
            Inventario inventario = fachada.ObtenerProducto(Id);

            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem() { Selected = false, Text = "Xbox", Value = "Xbox" });
            items.Add(new SelectListItem() { Selected = false, Text = "PsVita", Value = "Psvita" });
            items.Add(new SelectListItem() { Selected = false, Text = "PS3", Value = "PS3" });
            items.Add(new SelectListItem() { Selected = false, Text = "Wii", Value = "Wii" });
            items.Add(new SelectListItem() { Selected = false, Text = "3DS", Value = "3DS" });

            items.Find(x => x.Value.Equals(inventario.Producto.Consola)).Selected = true;

            //no borrar para despues poner imagen..
            //string pathRelative = inventario.Producto.PathImagen;
            //pathRelative = pathRelative.Substring(pathRelative.IndexOf("\\Images"));
            //inventario.Producto.PathImagen = pathRelative;
            ViewBag.lista = items;

            return View(inventario);
        }

        [HttpPost]
        public ActionResult EditarProducto(Inventario inventario)
        {
            AdminModel.IFachadaCatalogoProductos fachada = new AdminModel.CatalogoProductos();
            String path = Server.MapPath("~/_Documentos/Imagenes");
            fachada.ActualizarProducto(inventario,path);

         
            return RedirectToAction("ConsultarProductos");
        }

        public ActionResult ConsultarProductos() {
            List<Inventario> productos = null;

            AdminModel.IFachadaCatalogoProductos fachada = new AdminModel.CatalogoProductos();

            productos = fachada.ObtenerProductos();

            

            return View(productos);
        }

        public ActionResult EliminarProducto(int Id) {

            AdminModel.IFachadaCatalogoProductos fachada = new AdminModel.CatalogoProductos();
            fachada.BorrarProducto(Id,Server.MapPath("~/_Documentos/Imagenes"));

            return RedirectToAction("ConsultarProductos");
        }

       

    }
}
